<template>
  <div>
    <!--面包屑-->
    <breadcrumb></breadcrumb>
    <!-- 卡片-->
    <el-card class="box-card">
      <el-row :gutter="20">
        <search :list="msgList" :type="'msg'" @clearSearch="clearSearch" @searchData="searchData"></search>
      </el-row>
      <!-- 用户列表-->
      <el-table :data="dataList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="name" label="昵称"></el-table-column>
        <el-table-column prop="msgLength" label="消息量"></el-table-column>
        <el-table-column prop="date" label="时间"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!--查看按钮-->
            <el-button
              type="primary"
              icon="el-icon-view"
              size="mini"
              @click="lookup(scope.row._id)"
            ></el-button>
            <!--删除按钮-->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deletes(scope.row._id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
import breadcrumb from '../breadcrumb.vue'
import search from '../search.vue'
export default {
  data() {
    return {
      msgList: [],
      dataList: [],
      scope: ''
    }
  },
  created() {
    this.getMsg().then(async data => {
      await data.forEach(item => {
        item.msgLength = item.msg.length
        item.name = item.name.join()
      })
      this.msgList = data
      this.dataList = data
    })
  },
  methods: {
    // 获取所有消息
    async getMsg() {
      const res = await this.$http.post('/getupRoom', {})
      if (res.meta.status !== 200) {
        return this.$Message.error('获取消息失败')
      }
      return res.data
    },
    searchData(data) {
      this.dataList = data
    },
    clearSearch() {
      this.dataList = this.msgList
    },
    // 删除
    async deletes(scope) {
      this.comm.deletes(scope, '/deleteRoom/', this).then(() => {
        this.comm.ArrSplice(this.dataList, '_id', scope)
      })
    },
    // 查看
    lookup(scope) {
      this.$router.push({ path: '/suphome/showMsg', query: { id: scope } })
    }
  },
  components: {
    breadcrumb,
    search
  }
}
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 10px;
}
.el-table {
  margin-top: 15px;
}
</style>
